package com.accesshollywood.grfxOrdering.Dialogs;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.accesshollywood.grfxOrdering.basics.Destinations;
import com.accesshollywood.indexables.Destination;

public class NewDestinationDialog extends AbstractCustomDialog {
	private final JPanel contentPanel = new JPanel();
	private JTextField txtName;

	public NewDestinationDialog(Window w, ModalityType modality){
		super(w, modality);
		createDialog();
	}

	/**
	 * Create the dialog.
	 */
	public void createDialog() {
		setBounds(100, 100, 470, 106);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel);
			panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
			{
				JLabel lblNewLabel = new JLabel("Destination:");
				panel.add(lblNewLabel);
			}
			{
				txtName = new JTextField();
				txtName.setMaximumSize(new Dimension(2147483647, 20));
				txtName.setAlignmentX(Component.RIGHT_ALIGNMENT);
				panel.add(txtName);
				txtName.setColumns(10);
			}
		}
		
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						OK();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						cancel();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		
		setLocationRelativeTo(null);
		
		this.setVisible(true);
	}
	
	public void OK(){
		makeDestination();
		this.dispose();
	}
	
	public void cancel(){
		this.dispose();
	}
	
	public void makeDestination(){
		Destination newDestination = new Destination();
		newDestination.setName(txtName.getText());

		Destinations.addDestinationToLists(newDestination);
		Destinations.addDestinationToSQL(newDestination);
	}
}
